package com.yiwenup.leetcode.daily;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.daily
 * @Author yiwenup
 * @Date 2024-12-15 14:08:28
 * @Description https://leetcode.cn/problems/reduce-array-size-to-the-half/?envType=daily-question&envId=2024-12-15
 */
public class No1338 {
    public int minSetSize(int[] arr) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < arr.length; i++) {
            Integer count = map.get(arr[i]);
            if(count == null) {
                map.put(arr[i], 1);
            } else {
                map.put(arr[i], ++count);
            }
        }
        List<Integer> sort = new ArrayList<>(map.values());
        Collections.sort(sort);
        int ans = 0;
        int count = 0;
        for(int i = sort.size() - 1; i >= 0; i--) {
            if(count >= arr.length / 2) {
                break;
            }
            ans++;
            count += sort.get(i);
        }
        return ans;
    }
}
